package com.project.springboot.repository;

import com.project.springboot.dataobject.UserInfo;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

import java.util.List;
import java.util.Map;

/**
 * Description：用户信息接口
 * Created by 朱正磊 on 2019-03-16 19:12
 */
public interface UserInfoRepository extends JpaRepository<UserInfo, Integer> {

    Page<UserInfo> findByAge(Integer age, Pageable pageable);

    /**
     * description：SQL语句多表查询，使用Object接收查询结果集（不建议）
     * user 朱正磊
     * time 2019/3/18 0018 下午 1:36
     * @param departmentId 部门ID
     * @return 返回查询结果集
     */
    @Query(nativeQuery = true,
            value = "select ui.*, td.department_name " +
                    "from user_info ui " +
                    "left join t_department td " +
                    "on ui.department_id = td.department_id where td.department_id = :departmentId")
    List<Object> findUserInfoVOList_1(@Param("departmentId") Integer departmentId);

    /**
     * description：SQL语句多表查询，使用Map接收查询结果集（建议）
     * user 朱正磊
     * time 2019/3/18 0018 下午 1:36
     * @param departmentId 部门ID
     * @return 返回查询结果集
     */
    @Query(nativeQuery = true,
            value = "select ui.user_id userId, ui.user_name userName, td.department_name departmentName " +
                    "from user_info ui " +
                    "left join t_department td " +
                    "on ui.department_id = td.department_id where td.department_id = :departmentId")
    List<Map<String, Object>> findUserInfoVOList_2(@Param("departmentId") Integer departmentId);

}
